package com.minhua.jijiehao2.mainframe.account.accountFragment.impl

import android.text.TextUtils
import com.minhua.jijiehao2.base.BaseMvpPresenter
import com.minhua.jijiehao2.bean.AllAccountBean
import com.minhua.jijiehao2.bean.BaseBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.http.CallBack
import com.minhua.jijiehao2.http.ParamsMap
import com.minhua.jijiehao2.mainframe.account.accountFragment.imp.IAccountPresenter
import com.minhua.jijiehao2.mainframe.account.accountFragment.imp.IAccountView
import com.minhua.jijiehao2.utils.SPUtils

class AccountPresenter : BaseMvpPresenter<IAccountView>(), IAccountPresenter {

    private val mAccountModel = AccountModel()

    override fun getAccountList() {
        val userId = SPUtils.get(DefineKey.USER_ID, "") as String
        if (TextUtils.isEmpty(userId)) {
            v?.onAccountListSuccess(AllAccountBean(0f, listOf()))
            return
        }
        val map = ParamsMap()
        map.put(DefineKey.USER_ID, userId)
        mAccountModel.getAllAccount(map, object : CallBack<AllAccountBean>() {
            override fun onSuccess(t: BaseBean<AllAccountBean>) {
                v?.onAccountListSuccess(t.data!!)
            }
        })
    }

    override fun doDelete(id: Int) {
        val map = ParamsMap()
        map.put(DefineKey.ID, id)
        mAccountModel.deleteAccount(map, object : CallBack<Any>() {
            override fun onSuccess(t: BaseBean<Any>) {
                v?.onDeleteSuccess()
            }
        })
    }

    override fun doRepay(id: Int, status: Int) {
        val map = ParamsMap()
        map.put(DefineKey.ID, id)
        val statusInt = if (status == 0) {
            1
        } else {
            0
        }
        map.put("repayStatus", statusInt)

        mAccountModel.repayAccount(map, object : CallBack<Any>() {
            override fun onSuccess(t: BaseBean<Any>) {
                v?.onRepaySuccess()
            }
        })
    }
}